package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.BaseListData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Coupon;
import com.cskaoyan.model.promote.bo.CouponCreateBO;
import com.cskaoyan.model.promote.vo.CouponCreateVO;
import com.cskaoyan.model.promote.vo.CouponUpdateVO;
import com.cskaoyan.service.PromoteService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 推广管理之优惠卷管理 增删改查
 *
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    PromoteService promoteService;

    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:coupon:list"}, logical = Logical.OR)
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String name, Short type, Short status){
        BaseListData data = promoteService.queryCoupons(page, limit, sort, order, name, type,status);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:coupon:update"}, logical = Logical.OR)
    public BaseRespVo update(@RequestBody Coupon coupon){
        CouponUpdateVO couponUpdateVO = promoteService.updateCoupon(coupon);
        return BaseRespVo.ok(couponUpdateVO);
    }

    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:coupon:create"}, logical = Logical.OR)
    public BaseRespVo create(@RequestBody CouponCreateBO coupon){
        CouponCreateVO createVO = promoteService.createCoupon(coupon);
        return BaseRespVo.ok(createVO);

    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:coupon:delete"}, logical = Logical.OR)
    public BaseRespVo delete(@RequestBody Map map){
        promoteService.deleteCoupon(map);
        return BaseRespVo.ok();
    }

    /**
     * 1。根据传入优惠券id（couponId）
     * 2。查询优惠卷表
     * 3。返回单个优惠券信息
     */
    @RequestMapping("read")
    @RequiresPermissions(value = {"*", "admin:coupon:read"}, logical = Logical.OR)
    public BaseRespVo read(Integer id){
        Map map = promoteService.queryCoupon(id);
        return BaseRespVo.ok(map);
    }

    /**
     * 1。根据传入的map
     * 2。查询优惠卷_用户表
     * 3。返回用户信息
     */
    @RequestMapping("listuser")
    @RequiresPermissions(value = {"*", "admin:coupon:listuser"}, logical = Logical.OR)
    public BaseRespVo read(@RequestParam Map map){
        BaseListData data = promoteService.queryCouponUsers(map);
        return BaseRespVo.ok(data);
    }

}
